package com.hkp.face.web;


import com.hkp.face.entity.Vo.VisitorsVo;
import com.hkp.face.modal.dto.ProfileUploadDTO;
import com.hkp.face.result.ObjectOkResult;
import com.hkp.face.utils.Base64Utils;
import com.hkp.face.utils.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;


/**
 * 访客管理
 *
 * @author tangj
 * @date 2018/1/27 14:43
 */
@Controller
@RequestMapping("publics/visitor")
public class VisitorController {
    private static final Logger logger = LoggerFactory.getLogger(VisitorController.class);

    /**
     * 数据中间站
     */
    private VisitorsVo visitor = new VisitorsVo();

    @Value("${TbVisitor.uploadPath}")
    private String uploadPath;

    @Value("${TbVisitor.Path}")
    private String path;

    @Value("${TbVisitor.DeletePath}")
    private String deletePath;


    @Value("${VISITOR_FACE.GROP_ID}")
    private String visitorFaceGropId;

    @GetMapping(value = "")
    public String index() {
        return "public/publics_list";
    }

    @GetMapping(value = "/index")
    public String indexs() {
        return "index";
    }

    @GetMapping(value = "new")
    public String newPage() {
        return "public/publics_edit";
    }


    @CrossOrigin
    @PostMapping("upload")
    public @ResponseBody
    BaseResult upload(@RequestBody ProfileUploadDTO profileUploadDTO) {
        String imageName ="00"+(int) (1 + Math.random() * (1000 - 99 + 1));
        String imgStr = profileUploadDTO.img;
        //src本地地址
        String filePath = uploadPath + "/" + imageName + ".png";
        ///admin相对路径
        String paths = path + "/" + imageName + ".png";
        if (visitor.getFacePhotos() == null) {
            visitor.setFacePhotos(paths);
        } else {

            File file = new File(deletePath + visitor.getFacePhotos());
            if (file.exists()) {
                file.delete();
            }
            visitor.setFacePhotos(null);
            visitor.setFacePhotos(paths);

        }
        ObjectOkResult r = new ObjectOkResult(paths);

        Base64Utils.createFile(imgStr, new File(filePath));
        return r;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public @ResponseBody
    BaseResult delete(@RequestBody Map dataMap) {
        ObjectOkResult r = new ObjectOkResult();
        Set<String> keys = dataMap.keySet();
        for (String key : keys) {
            String filePath = uploadPath + "/" + dataMap.get(key);
            File file = new File(filePath);
            if (file.exists()) {
                file.delete();
            }
        }
        return r;
    }


    @RequestMapping(value = "/cancel", method = RequestMethod.POST)
    public @ResponseBody
    BaseResult cancel() {
        ObjectOkResult r = new ObjectOkResult();
        if (visitor != null) {
            File file = new File(deletePath + visitor.getFacePhotos());
            if (file.exists()) {
                file.delete();
            }
        }
        //对象清空
        visitor = null;
        return r;
    }

    @RequestMapping(value = "/instat", method = RequestMethod.POST)
    public @ResponseBody BaseResult instat() throws IOException {
        ObjectOkResult r = new ObjectOkResult();
        try {
            System.out.println(deletePath + visitor.getFacePhotos());
        } catch (Exception e) {
            String msg = e.getMessage();
            logger.error(msg);
            return ObjectOkResult.fail(msg, 403);
        }
        return r;
    }

    @GetMapping(value = "index")
    public String home() {

        return "index";
    }
}
